package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.bo.dto.NameValueSelectDto;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsModalSelector;
import com.ys.enume.base.TbFileInfoEnum;
import com.ys.enume.sys.YsColEnum;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.sys.YsColMapper;
import com.ys.service.sys.YsColService;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("ysColService")
public class YsColServiceImpl implements YsColService {

	@Autowired
	private YsColMapper ysColMapper;

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsMenu> listYsMenu = new YsMenu()
				.setOrderby("A.Lev asc, A.SortNo asc")
				.queryList();
		
		// 一级菜单
		for(YsMenu ysMenu1 : listYsMenu) {
			if (ysMenu1.getLev() != YsMenuEnum.Lev.ONE.value) {
				continue;
			}
			String id1 = ysMenu1.getId().toString();
			String title1 = ysMenu1.getName();
			String parentId1 = ysMenu1.getPid().toString();
			
			// 二级菜单
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			for (YsMenu ysMenu2 : listYsMenu) {
				if (ysMenu2.getPid().longValue() != ysMenu1.getId().longValue()) {
					continue;
				}
				String id2 = ysMenu2.getId().toString();
				String title2 = ysMenu2.getName();
				String parentId2 = ysMenu2.getPid().toString();
				
				// 三级菜单
				List<DtreeSqlDto.DataDto> children1 = new ArrayList<DtreeSqlDto.DataDto>();
				for (YsMenu ysMenu3 : listYsMenu) {
					if (ysMenu3.getPid().longValue() != ysMenu2.getId().longValue()) {
						continue;
					}
					String id3 = ysMenu3.getId().toString();
					String title3 = ysMenu3.getName();
					String parentId3 = ysMenu3.getPid().toString();
					List<DtreeSqlDto.DataDto> children2 = null;
					DtreeSqlDto.DataDto dto = new DtreeSqlDto.DataDto(id3, title3, parentId3, DtreeSqlDto.DataDto.initCheckArr(null), children2);
					children1.add(dto);
				}
				
				DtreeSqlDto.DataDto dataDto2 = new DtreeSqlDto.DataDto(id2, title2, parentId2, DtreeSqlDto.DataDto.initCheckArr(null), children1);
				children.add(dataDto2);
			}
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}
	
	@Override
	public R add(Long menuId, String code, String name, Integer authType, Integer frontFixed, Integer frontWidth,
			Integer sortNo, String frontType, Integer frontShow, Integer frontEdit, Integer frontConditionState,
			String frontConditionType, String frontConditionRangeType, String frontConditionCode, String frontConditionDefault,Integer frontExport) {
		
		
		if (YsColEnum.FrontType.checkIn(frontType) == false) {
			return R.error("请选择正确的字段类型");
		}
		
		Integer frontAlign = null;
		String dictKeyBase = null;
		Integer formLength = null;
		String formTip = null;
		Integer formAddState = YsColEnum.FormAddState.NO.value;
		String formAddType = YsColEnum.FormAddType.TEXT.value;
		Integer formAddRequired = YsColEnum.FormAddRequired.NO.value;
		Integer formAddReadonly = YsColEnum.FormAddReadonly.NO.value;
		Integer formAddHidden = YsColEnum.FormAddHidden.NO.value;
		Integer formEditState = YsColEnum.FormEditState.NO.value;
		String formEditType = YsColEnum.FormEditType.TEXT.value;
		Integer formEditRequired = YsColEnum.FormEditRequired.NO.value;
		Integer formEditReadonly = YsColEnum.FormEditReadonly.NO.value;
		Integer formEditHidden = YsColEnum.FormEditHidden.NO.value;
		Integer formDetailState = YsColEnum.FormDetailState.NO.value;
		String formDetailType = YsColEnum.FormDetailType.TEXT.value;
		Integer importState = YsColEnum.ImportState.NO.value;
		Integer appFormState = YsColEnum.FormEditState.NO.value;
		String appFormType = YsColEnum.FormEditType.TEXT.value;
		Integer importSortNo = 99;
		String modalKey = null;
		String modalRelativeCode = null;
		String modalValueCode = null;
		String dropdownRequestPath = null;
		Integer multyFileSourceType = null;
		YsCol entity = YsCol.ysCol(
				menuId, name, code, sortNo, authType,
				frontType, frontEdit, frontShow, frontWidth, frontFixed,
				frontAlign, frontConditionState, frontConditionType, frontConditionRangeType, frontConditionCode,
				frontConditionDefault, dictKeyBase, formLength, formTip, formAddState, formAddType,
				formAddRequired, formAddReadonly, formAddHidden, formEditState, formEditType,
				formEditRequired, formEditReadonly, formEditHidden, formDetailState, formDetailType,
				importState, importSortNo,appFormState,appFormType,
				modalKey, modalRelativeCode, modalValueCode, dropdownRequestPath, multyFileSourceType, 1,frontExport);
		ysColMapper.insert(entity);
		return R.ok("操作成功");
	}

	@Override
	public R deleteTrue(Long id) {
		ysColMapper.deleteTrue(id);
		return R.ok();
	}

	@Override
	public String toSettingOption(Model model, Long colId) {
		
		if (colId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		if (!YsColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(entity.getFrontType())
				&& !YsColEnum.FrontType.SELECTOR_SWITCH.value.equals(entity.getFrontType())
				&& !YsColEnum.FormAddType.DROPDOWN_SINGLE.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormAddType.SELECTOR_SWITCH.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormEditType.DROPDOWN_SINGLE.value.equals(entity.getFormEditType())
				&& !YsColEnum.FormEditType.SELECTOR_SWITCH.value.equals(entity.getFormEditType())) {
			return W.error(model, "非下拉框或转换框，无法设置");
		}
		
		// 字典数组
		List<ToSettingOptionDto.RowDto> rows = new ArrayList<ToSettingOptionDto.RowDto>();
		String dictKeyBase = entity.getDictKeyBase();
		if (dictKeyBase != null && dictKeyBase.length() > 0) {
			
			List<YsDict> listYsDict = new YsDict().setDictKey(dictKeyBase)
					.setOrderby("A.SortNo asc")
					.queryList();
			
			for(YsDict ysDict : listYsDict) {
				Long id2 = ysDict.getId();
				String dictName = ysDict.getDictName();
				String dictValue = ysDict.getDictValue();
				rows.add(new ToSettingOptionDto.RowDto(id2, dictName, dictValue));
			}
		}
		
		String name = entity.getName();
		String code = entity.getCode();
		ToSettingOptionDto dto = new ToSettingOptionDto(colId, name, code, dictKeyBase, rows);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toSettingOption";
	}

	@Override
	public String toSettingDropdownMulty(Model model, Long colId) {
		
		if (colId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		if (!YsColEnum.FormAddType.DROPDOWN_MULTY.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormEditType.DROPDOWN_MULTY.value.equals(entity.getFormEditType())) {
			return W.error(model, "非下拉框(多选)类型无法设置");
		}
		
		String name = entity.getName();
		String code = entity.getCode();
		String dropdownRequestPath = entity.getDropdownRequestPath();
		ToSettingDropdownMultyDto dto = new ToSettingDropdownMultyDto(colId, name, code, dropdownRequestPath);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toSettingDropdownMulty";
	}

	@Override
	public String toSettingForm(Model model, Long menuId) {
		
		if (menuId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsMenu entity = new YsMenu().setId(menuId).queryFirst();
		if (entity.getLev() != YsMenuEnum.Lev.THREE.value) {
			return W.error(model, "非三级菜单无法设置");
		}
		
		// 新增表单数组
		List<ToSettingFormDto.RowDto> rowsAdd = new ArrayList<ToSettingFormDto.RowDto>();
		List<YsCol> listYsCol = new YsCol().setMenuId(menuId).setFormAddState(YsColEnum.FormAddState.YES.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		for(YsCol ysCol : listYsCol) {
			Long id = ysCol.getId();
			String code = ysCol.getCode();
			String name = ysCol.getName();
			List<NameValueSelectDto> typeArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddType e : YsColEnum.FormAddType.values()) {
				boolean isSelected = e.value.equals(ysCol.getFormAddType());
				typeArr.add(new NameValueSelectDto(e.name, e.value, isSelected));
			}
			List<NameValueSelectDto> stateArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddState e : YsColEnum.FormAddState.values()) {
				boolean isSelected = e.value == ysCol.getFormAddState();
				stateArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			List<NameValueSelectDto> requiredArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddRequired e : YsColEnum.FormAddRequired.values()) {
				boolean isSelected = e.value == ysCol.getFormAddRequired();
				requiredArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			rowsAdd.add(new ToSettingFormDto.RowDto(id, code, name, typeArr, stateArr, requiredArr));
		}
		
		// 编辑表单数组
		List<ToSettingFormDto.RowDto> rowsEdit = new ArrayList<ToSettingFormDto.RowDto>();
		List<YsCol> listYsCol2 = new YsCol().setMenuId(menuId).setFormEditState(YsColEnum.FormEditState.YES.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		for(YsCol ysCol : listYsCol2) {
			Long id = ysCol.getId();
			String code = ysCol.getCode();
			String name = ysCol.getName();
			List<NameValueSelectDto> typeArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditType e : YsColEnum.FormEditType.values()) {
				boolean isSelected = e.value.equals(ysCol.getFormEditType());
				typeArr.add(new NameValueSelectDto(e.name, e.value, isSelected));
			}
			List<NameValueSelectDto> stateArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditState e : YsColEnum.FormEditState.values()) {
				boolean isSelected = e.value == ysCol.getFormEditState();
				stateArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			List<NameValueSelectDto> requiredArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditRequired e : YsColEnum.FormEditRequired.values()) {
				boolean isSelected = e.value == ysCol.getFormEditRequired();
				requiredArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			rowsEdit.add(new ToSettingFormDto.RowDto(id, code, name, typeArr, stateArr, requiredArr));
		}
		
		ToSettingFormDto dto = new ToSettingFormDto(menuId, rowsAdd, rowsEdit);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toSettingOption";
	}

	@Override
	public String toSettingSelectorModal(Model model, Long id) {

		YsCol entity = new YsCol().setId(id).queryFirst();
		
		// 弹出框信息
		ToSettingSelectorModalDto.SelectorModalInfoDto selectorModalInfo = null;
		{
			// 弹出框选项数组
			Long idYsModalSelector = null;
			String modalKey = entity.getModalKey();
			if (modalKey != null && modalKey.length() > 0) {

				YsModalSelector ysModalSelector = new YsModalSelector().setNameTable(modalKey).queryFirst();
				if (ysModalSelector == null) {
					return W.error(model, "弹出框ModalKey有误，请检查");
				}
				idYsModalSelector = ysModalSelector.getId();
			}
			
			List<NameValueSelectDto> modalArr = new ArrayList<NameValueSelectDto>();
			List<YsModalSelector> listYsModalSelector = new YsModalSelector().queryList();
			for(YsModalSelector ysModalSelector : listYsModalSelector) {
				String name = ysModalSelector.getName() + "(" + ysModalSelector.getNameTable() + ")";
				String value = ysModalSelector.getId().toString();
				boolean isSelected = idYsModalSelector != null ? ysModalSelector.getId().longValue() == idYsModalSelector : false;
				modalArr.add(new NameValueSelectDto(name, value, isSelected));
			}
			
			selectorModalInfo = new ToSettingSelectorModalDto.SelectorModalInfoDto(modalArr);
		}

		String name = entity.getName();
		String code = entity.getCode();
		ToSettingSelectorModalDto dto = new ToSettingSelectorModalDto(id, name, code, selectorModalInfo);
		model.addAttribute("dto", dto);
		
		return "sys/ysCol/toSettingSelectorModal";
	}

	@Override
	public R settingOption(Long colId, String dictKeyBase) {
		
		if (colId == null) {
			return R.error("ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		} else if (!YsColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(entity.getFrontType())
				&& !YsColEnum.FrontType.SELECTOR_SWITCH.value.equals(entity.getFrontType())
				&& !YsColEnum.FormAddType.DROPDOWN_SINGLE.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormAddType.SELECTOR_SWITCH.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormEditType.DROPDOWN_SINGLE.value.equals(entity.getFormEditType())
				&& !YsColEnum.FormEditType.SELECTOR_SWITCH.value.equals(entity.getFormEditType())) {
			return R.error("非下拉框或转换框，无法设置");
		}
		
		entity.setDictKeyBase(dictKeyBase);
		ysColMapper.update(entity);
		return R.ok();
	}

	@Override
	public R settingDropdownMulty(Long colId, String dropdownRequestPath) {
		
		if (colId == null) {
			return R.error("ID不能为空");
		} else if (dropdownRequestPath == null || dropdownRequestPath.length() == 0) {
			return R.error("请求路径不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		}
		
		entity.setDropdownRequestPath(dropdownRequestPath);
		ysColMapper.update(entity);
		return R.ok();
	}

	@Override
	public R settingSelectorModal(Long id, Long idYsModalSelector) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		if (idYsModalSelector == null) {
			return R.error("弹出框不能为空");
		}

		YsCol entity = new YsCol().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		}
		
		YsModalSelector ysModalSelector = new YsModalSelector().setId(idYsModalSelector).queryFirst();
		entity.setModalKey(ysModalSelector.getNameTable());
		ysColMapper.update(entity);

		return R.ok();
	}

	@Override
	public String toSettingMultyFileSourceType(Model model, Long colId) {
		
		if (colId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		if (!YsColEnum.FormAddType.FILE_MULTY.value.equals(entity.getFormAddType())
				&& !YsColEnum.FormEditType.FILE_MULTY.value.equals(entity.getFormEditType())) {
			return W.error(model, "非多文件类型无法设置");
		}
		
		// 字典数组
		List<NameValueSelectDto> rows = new ArrayList<NameValueSelectDto>();
		{
			String name = "请选择";
			String value = "";
			boolean isSelected = entity.getMultyFileSourceType() == null ? true : false;
			rows.add(new NameValueSelectDto(name, value, isSelected));
		}
		for(TbFileInfoEnum.SourceType e : TbFileInfoEnum.SourceType.values()) {
			String name = e.name;
			String value = e.value.toString();
			boolean isSelected = entity.getMultyFileSourceType() == null ? false : e.value == entity.getMultyFileSourceType();
			rows.add(new NameValueSelectDto(name, value, isSelected));
		}

		String name = entity.getName();
		String code = entity.getCode();
		ToSettingMultyFileSourceTypeDto dto = new ToSettingMultyFileSourceTypeDto(colId, name, code, rows);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toSettingMultyFileSourceType";
	}

	@Override
	public R settingMultyFileSourceType(Long id, Integer multyFileSourceType) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		if (multyFileSourceType == null) {
			return R.error("来源类型不能为空");
		} else if (TbFileInfoEnum.SourceType.checkIn(multyFileSourceType) == false) {
			return R.error("来源类型不在指定范围内");
		}

		YsCol entity = new YsCol().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		}
		
		entity.setMultyFileSourceType(multyFileSourceType);
		ysColMapper.update(entity);

		return R.ok();
	}

	@Override
	public R settingImport(Long id, Integer importState) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		YsCol entity = new YsCol().setId(id).queryFirst();
		entity.setImportState(importState);
		ysColMapper.update(entity);
		return R.ok();
	}
}
	

